// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.radio;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import zombie.core.Language;
import zombie.core.Translator;

public final class RadioTranslationData {
    private String filePath;
    private String guid;
    private String language;
    private Language languageEnum;
    private int version = -1;
    private final ArrayList<String> translators = new ArrayList();
    private final Map<String, String> translations = new HashMap();

    public RadioTranslationData(String file) {
        this.filePath = file;
    }

    public String getFilePath() {
        return this.filePath;
    }

    public String getGuid() {
        return this.guid;
    }

    public String getLanguage() {
        return this.language;
    }

    public Language getLanguageEnum() {
        return this.languageEnum;
    }

    public int getVersion() {
        return this.version;
    }

    public int getTranslationCount() {
        return this.translations.size();
    }

    public ArrayList<String> getTranslators() {
        return this.translators;
    }

    public boolean validate() {
        return this.guid != null && this.language != null && this.version >= 0;
    }

    public boolean loadTranslations() {
        boolean boolean0 = false;
        if (Translator.getLanguage() != this.languageEnum) {
            System.out.println("Radio translations trying to load language that is not the current language...");
            return false;
        } else {
            try {
                File file = new File(this.filePath);
                if (file.exists() && !file.isDirectory()) {
                    BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(new FileInputStream(this.filePath), Charset.forName(this.languageEnum.charset()))
                    );
                    String string0 = null;
                    boolean boolean1 = false;
                    ArrayList arrayList = new ArrayList();

                    while ((string0 = bufferedReader.readLine()) != null) {
                        string0 = string0.trim();
                        if (string0.equals("[Translations]")) {
                            boolean1 = true;
                        } else if (boolean1) {
                            if (!string0.equals("[Collection]")) {
                                if (string0.equals("[/Translations]")) {
                                    boolean0 = true;
                                    break;
                                }

                                String[] string1 = string0.split("=", 2);
                                if (string1.length == 2) {
                                    String string2 = string1[0].trim();
                                    String string3 = string1[1].trim();
                                    this.translations.put(string2, string3);
                                }
                            } else {
                                String string4 = null;

                                while ((string0 = bufferedReader.readLine()) != null) {
                                    string0 = string0.trim();
                                    if (string0.equals("[/Collection]")) {
                                        break;
                                    }

                                    String[] string5 = string0.split("=", 2);
                                    if (string5.length == 2) {
                                        String string6 = string5[0].trim();
                                        String string7 = string5[1].trim();
                                        if (string6.equals("text")) {
                                            string4 = string7;
                                        } else if (string6.equals("member")) {
                                            arrayList.add(string7);
                                        }
                                    }
                                }

                                if (string4 != null && arrayList.size() > 0) {
                                    for (String string8 : arrayList) {
                                        this.translations.put(string8, string4);
                                    }
                                }

                                arrayList.clear();
                            }
                        }
                    }
                }
            } catch (Exception exception) {
                exception.printStackTrace();
                boolean0 = false;
            }

            return boolean0;
        }
    }

    public String getTranslation(String _guid) {
        return this.translations.containsKey(_guid) ? (String)this.translations.get(_guid) : null;
    }

    public static RadioTranslationData ReadFile(String file) {
        RadioTranslationData radioTranslationData = new RadioTranslationData(file);
        File _file = new File(file);
        if (_file.exists() && !_file.isDirectory()) {
            try {
                FileInputStream fileInputStream = new FileInputStream(file);

                try {
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
                    String string0 = null;

                    while ((string0 = bufferedReader.readLine()) != null) {
                        String[] string1 = string0.split("=");
                        if (string1.length > 1) {
                            String string2 = string1[0].trim();
                            String string3 = "";

                            for (int _int = 1; _int < string1.length; _int++) {
                                string3 = string3 + string1[_int];
                            }

                            string3 = string3.trim();
                            if (string2.equals("guid")) {
                                radioTranslationData.guid = string3;
                            } else if (string2.equals("language")) {
                                radioTranslationData.language = string3;
                            } else if (string2.equals("version")) {
                                radioTranslationData.version = Integer.parseInt(string3);
                            } else if (string2.equals("translator")) {
                                String[] string4 = string3.split(",");
                                if (string4.length > 0) {
                                    for (String string5 : string4) {
                                        radioTranslationData.translators.add(string5);
                                    }
                                }
                            }
                        }

                        string0 = string0.trim();
                        if (string0.equals("[/Info]")) {
                            break;
                        }
                    }
                } catch (Throwable throwable0) {
                    try {
                        fileInputStream.close();
                    } catch (Throwable throwable1) {
                        throwable0.addSuppressed(throwable1);
                    }

                    throw throwable0;
                }

                fileInputStream.close();
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }

        boolean _boolean = false;
        if (radioTranslationData.language != null) {
            for (Language languagex : Translator.getAvailableLanguage()) {
                if (languagex.toString().equals(radioTranslationData.language)) {
                    radioTranslationData.languageEnum = languagex;
                    _boolean = true;
                    break;
                }
            }
        }

        if (!_boolean && radioTranslationData.language != null) {
            System.out.println("Language " + radioTranslationData.language + " not found");
            return null;
        } else {
            return radioTranslationData.guid != null && radioTranslationData.language != null && radioTranslationData.version >= 0
                ? radioTranslationData
                : null;
        }
    }
}
